// <copyright file="SimpleEditor.cs" company="YUIAsp.Net">
// Copyright (c) 2009 All Right Reserved
// </copyright>
// <author>raul popescu</author>
// <email>dev@yuiasp.net</email>
// <date>2009-01-30</date>
// <summary>YUIAsp.Net File</summary>

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using YUIAspNet.Common;

#region Embedded resources definitions
[assembly: WebResource("YUIAspNet.Editor.SimpleEditor.js", "text/javascript")]
[assembly: WebResource("YUIAspNet.EmbeddedSkins.DefaultSkin.Editor.simpleeditor-core.css", "text/css", PerformSubstitution = true)]
[assembly: WebResource("YUIAspNet.EmbeddedSkins.DefaultSkin.Editor.simpleeditor-skin.css", "text/css", PerformSubstitution = true)]
[assembly: WebResource("YUIAspNet.EmbeddedSkins.DefaultSkin.Editor.blankimage.png", "image/png")]
[assembly: WebResource("YUIAspNet.EmbeddedSkins.DefaultSkin.Editor.editor-knob.gif", "image/gif")]
[assembly: WebResource("YUIAspNet.EmbeddedSkins.DefaultSkin.Editor.editor-sprite-active.gif", "image/gif")]
[assembly: WebResource("YUIAspNet.EmbeddedSkins.DefaultSkin.Editor.editor-sprite.gif", "image/gif")]

#endregion

namespace YUIAspNet
{
    /// <summary>
    /// Simple editor control based on YUI Simple editor widget.
    /// </summary>
    [ToolboxItem(true)]
    [ParseChildren(true)]
    public class SimpleEditor : YUIScriptBaseControl, INamingContainer, IPostBackDataHandler
    {
        private const int DEFAULT_WIDTH = 500;
        private const int DEFAULT_HEIGHT = 300;

        private TextBox _tbInput;

        private Unit _width = new Unit(DEFAULT_WIDTH, UnitType.Pixel);
        private Unit _height = new Unit(DEFAULT_HEIGHT, UnitType.Pixel);

        /// <summary>
        /// Initializes a new instance of the <see cref="SimpleEditor"/> class.
        /// </summary>
        public SimpleEditor()
        {
            this.Init += new EventHandler(SimpleEditor_Init);

            _tbInput = new TextBox();
            _tbInput.ID = "input";
            _tbInput.TextMode = TextBoxMode.MultiLine;

            MaxUndo = 30;
        }

        void SimpleEditor_Init(object sender, EventArgs e)
        {
            this.Controls.Add(_tbInput);
        }

        /// <summary>
        /// Registers the style sheets for the current control.
        /// Must be overriden in the parent control to add any css file reference.
        /// </summary>
        protected override void RegisterStyleSheets()
        {
            //TODO: yui skin + core?
            this.RegisterEmbeddedStylesheet("Editor", "simpleeditor-core.css");
            this.RegisterEmbeddedStylesheet("Editor", "simpleeditor-skin.css");
        }

        /// <summary>
        /// Register the values to send to the client control for initialization.
        /// </summary>
        protected override void RegisterClientInitialization() 
        {
            AddProperty("InputID", _tbInput.ClientID);
            AddProperty("Width", this.Width.ToString());
            AddProperty("Height", this.Height.ToString());
            AddProperty("ShowDomPath", this.ShowDomPath);
            AddProperty("Enabled", this.Enabled);
            AddProperty("ExtraCss", ExtraCss);
            AddProperty("FocusAtStart", FocusAtStart);
            AddProperty("RequireSelection", RequireSelection);
            AddProperty("PreserveInitialFormat", PreserveInitialFormat);
        }

        /// <summary>
        /// Registers any additional scripts to include for this control.
        /// </summary>
        protected override void RegisterScriptReferences()
        {
            AddScriptReference(new ScriptReference("YUIAspNet.Editor.SimpleEditor.js", this.GetType().Assembly.FullName));
        }

        #region Public properties
        /// <summary>
        /// Gets or sets a value indicating whether to show DOM path of the current node.
        /// </summary>
        /// <value><c>true</c> to show the DOM path; otherwise, <c>false</c>.</value>
        public bool ShowDomPath { get; set; }

        /// <summary>
        /// Gets or sets the extra CSS for customize display of HTML elements inside the editor.
        /// </summary>
        /// <value>The extra CSS.</value>
        public string ExtraCss { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether this control is focused at start.
        /// </summary>
        /// <value><c>true</c> to focus at start; otherwise, <c>false</c>.</value>
        public bool FocusAtStart { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether selection is required in order to apply formatting to the HTML inside the editor.
        /// </summary>
        /// <value><c>true</c> to require selection; otherwise, <c>false</c>.</value>
        public bool RequireSelection { get; set; }

        /// <summary>
        /// Gets or sets the max undo items to track.
        /// </summary>
        /// <value>The max undo.</value>
        public int MaxUndo { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether to preserve initial format.
        /// </summary>
        /// <value>
        /// 	<c>true</c> to preserve initial format; otherwise, <c>false</c>.
        /// </value>
        public bool PreserveInitialFormat { get; set; }

        /// <summary>
        /// Use &lt;p&gt; tag instead of the default, &lt;br&gt;
        /// </summary>
        public bool UseParagraph { get; set; }

        /// <summary>
        /// Gets or sets the HTML inside the control.
        /// </summary>
        /// <value>The HTML.</value>
        public string HTML
        {
            get { return _tbInput.Text; }
            set { _tbInput.Text = value; }
        }

        /// <summary>
        /// Gets or sets the height of the Web server control.
        /// </summary>
        /// <value></value>
        /// <returns>A <see cref="T:System.Web.UI.WebControls.Unit"/> that represents the height of the control. The default is <see cref="F:System.Web.UI.WebControls.Unit.Empty"/>.</returns>
        /// <exception cref="T:System.ArgumentException">The height was set to a negative value.</exception>
        public override Unit Height
        {
            get { return _height; }
            set { _height = value; }
        }

        /// <summary>
        /// Gets or sets the width of the Web server control.
        /// </summary>
        /// <value></value>
        /// <returns>A <see cref="T:System.Web.UI.WebControls.Unit"/> that represents the width of the control. The default is <see cref="F:System.Web.UI.WebControls.Unit.Empty"/>.</returns>
        /// <exception cref="T:System.ArgumentException">The width of the Web server control was set to a negative value. </exception>
        public override Unit Width
        {
            get { return _width; }
            set { _width = value; }
        }

        #endregion

        #region IPostBackDataHandler Members

        /// <summary>
        /// When implemented by a class, processes postback data for an ASP.NET server control.
        /// </summary>
        /// <param name="postDataKey">The key identifier for the control.</param>
        /// <param name="postCollection">The collection of all incoming name values.</param>
        /// <returns>
        /// true if the server control's state changes as a result of the postback; otherwise, false.
        /// </returns>
        public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
        {
            //find position in request.form.
            return false;
        }

        /// <summary>
        /// When implemented by a class, signals the server control to notify the ASP.NET application that the state of the control has changed.
        /// </summary>
        public void RaisePostDataChangedEvent()
        {
            // no postbacks
        }

        #endregion
    }
}
